package com.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;

public class PropertyUtils {
	public static String getProperty(String fileName, String propName) throws Exception {
		InputStream in = new FileInputStream(getBinPath() + fileName + ".properties");
		Properties prop = new Properties();
		prop.load(in);
		String value = (String) prop.get(propName);
		in.close();
		return value;
	}

	public static Properties getProperties(String fileName) {
		ResourceBundle resourcebundle = ResourceBundle.getBundle(fileName);
		Enumeration<String> enumeration = resourcebundle.getKeys();
		Properties properties = new Properties();
		String propertyName;
		while (enumeration.hasMoreElements()) {
			propertyName = enumeration.nextElement();
			properties.put(propertyName, resourcebundle.getObject(propertyName));
		}
		return properties;
	}
	public static void setProperty(String fileName, String propName, String propValue) throws Exception {
		Properties prop = getProperties(fileName);
		prop.setProperty(propName, propValue);
		FileOutputStream fos = new FileOutputStream(getBinPath() + fileName + ".properties");
		prop.store(fos, "");
		fos.close();
	}

	private static String getBinPath() {
		return  PropertyUtils.class.getResource("/").getPath();
	}
}
